package com.xq.shop.controller;


import com.xq.shop.dto.EditGoodsTypeDto;
import com.xq.shop.dto.IntegerDto;
import com.xq.shop.service.IGoodsTypeService;
import com.xq.shop.service.IUserService;
import com.xq.shop.vo.GoodsTypeVo;
import com.xq.shop.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import utils.BaseResult;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gaoxiaojin
 * @since 2024-07-18
 */
@RestController
@RequestMapping("/goods/type")
public class GoodsTypeController {


    private final IGoodsTypeService goodsTypeService;
    private final IUserService userService;

    @Autowired
    public GoodsTypeController(IGoodsTypeService goodsTypeService, IUserService userService) {
        this.goodsTypeService = goodsTypeService;
        this.userService = userService;
    }

    @RequestMapping("/getGoodsTypeTree")
    public BaseResult<List<GoodsTypeVo>> getMenuTree() {
        BaseResult<List<GoodsTypeVo>> result = new BaseResult<>();
        List<GoodsTypeVo> voList = goodsTypeService.getGoodsTypeTree();
        result.initializeSuccess(voList);
        return result;
    }

    @RequestMapping("/edit")
    public BaseResult<Void> editGoodsType(@RequestBody @Validated EditGoodsTypeDto dto, HttpServletRequest request) {
        UserVo userVo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        goodsTypeService.editGoodsType(dto, userVo.getId());
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/remove")
    public BaseResult<Void> removeGoodsType(@RequestBody @Validated IntegerDto dto, HttpServletRequest request) {
        UserVo userVo = userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        goodsTypeService.removeGoodsType(dto, userVo.getId());
        result.initializeSuccess();
        return result;
    }

}
